<?php
declare (strict_types = 1);

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\api\model\Policy as PolicyModel;

class Policy extends Base
{

    /**
     * 规则列表
     * @param $page 页面
     * @param $pagesize 每页大小
     * @return \think\response\Json
     */
    public function policylist($page,$pagesize){
        try {
            $PolicyModel = new PolicyModel();
            $list = $PolicyModel->PolicyList($page,$pagesize);
            return json($list);
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return json($error);
        }
    }

    /**
     * 新增规则
     * @param $policyname
     * @param int $status
     * @return \think\response\Json
     */
    public function policyadd($policyname,$status=1){
        try {
            $PolicyModel = new PolicyModel();
            $res = $PolicyModel->PolicyAdd($policyname,$status);
            return json($res);
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return json($error);
        }
    }

    /**
     * 保存规则
     * @param $policy_id
     * @param $policyname
     * @return \think\response\Json
     */
    public function policyedit($policy_id,$policyname){
        try {
            $PolicyModel = new PolicyModel();
            $res = $PolicyModel->PolicyEdit($policy_id,$policyname);
            return json($res);
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return json($error);
        }
    }

    /**
     * 规则启/禁用
     * @param $policy_id
     * @param $status
     * @return \think\response\Json
     */
    public function policysetstatus($policy_id,$status){
        try {
            $PolicyModel = new PolicyModel();
            $res = $PolicyModel->PolicySetStatus($policy_id,$status);
            return json($res);
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return json($error);
        }
    }

    /**
     * 规则删除
     * @param $policy_id
     * @return \think\response\Json
     */
    public function policydel($policy_id){
        try {
            $PolicyModel = new PolicyModel();
            $res = $PolicyModel->PolicyDel($policy_id);
            return json($res);
        } catch (\Exception $e) {
            $error = ['status'=>false,'code'=>$e->getCode(),'msg'=>$e->getMessage(),'data'=>''];
            return json($error);
        }
    }

}
